<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\jia;

use app\api\service\User as UserService;
use app\common\model\jia\SurveyLog as SurveyLogModel;

/**
 * 文章分类模型
 * Class Survey
 * @package app\api\model\article
 */
class SurveyLog extends SurveyLogModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'store_id',
        'update_time',
    ];

    public function getList($page, $size)
    {
        $filter = [];

        $start = ($page - 1) * $size;
        $end   = $page * $size;
        return $this->where($filter)
            ->order('create_time desc')
            ->limit($start, $end)
            ->select()->toArray();
    }

    public function add(array $list)
    {
        // 当前用户信息
        $user  = UserService::getCurrentLoginUser(true);
        $score = 0;
        foreach ($list as $key => $value) {
            if ($value['score'] > 0 && $value['type'] == 0) {
                $score += $value['score'];
                switch ($value['score']) {
                    case 10:
                        $list[$key]['answer'] = '满意';
                        break;
                    case 8:
                        $list[$key]['answer'] = '较满意';
                        break;
                    case 6:
                        $list[$key]['answer'] = '一般';
                        break;
                    case 4:
                        $list[$key]['answer'] = '不好';
                        break;
                    case 2:
                        $list[$key]['answer'] = '很差';
                        break;
                    default:
                        // code...
                        break;
                }
            }
            if ($value['score'] > 0 && $value['type'] == 2) {
                $score += $value['score'];
                switch ($value['score']) {
                    case 10:
                        $list[$key]['answer'] = '是';
                        break;
                    case 0:
                        $list[$key]['answer'] = '否';
                        break;

                    default:
                        // code...
                        break;
                }
            }
        }

        $this->save([
            'user_id'  => $user['user_id'],
            'score'    => $score,

            'content'  => json_encode($list),

            'store_id' => self::$storeId,
        ]);

        return true;
    }
}
